package com.shop2cn.iapi.livebuyer.testcase;

import java.util.Arrays;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.shop2cn.app.liveseller.parameter.CreateSellerLiveBean;
import com.shop2cn.iapi.livebuyer.parameter.QueryProductListBean;
import com.shop2cn.iapi.livebuyer.service.QueryProductListCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.ymatou.iapi.godmch.parameter.setAgentConditionMarkupConfigBean;
import com.ymatou.iapi.godmch.parameter.setConditionMarkupConfigBean;
import com.ymatou.iapi.godmch.service.setAgentConditionMarkupConfigCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.AuthCallServiceV2;
import com.ymttest.business.service.LiveRoomShop2cnCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.livesellerShop2cn.LiveRoom;
import com.ymttest.database.sqlwapper.livesellerShop2cn.LiveRoomShop2cnWapper;
/**
*获取某直播间商品列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_QueryProductList{
	private static QueryProductListBean queryproductlistBean;
	private static QueryProductListCall queryproductlistCall;
	private static LiveRoomShop2cnWapper liveRoomShop2nWapper;
	private static int sellerId ;
	private static String token ;
	private static int csId;
	private static String cstoken;
	@BeforeClass
	public static void setUp() throws Exception {
		Logger.createResultFile("获取某直播间商品列表");
		sellerId = GodmchManageCallService.CreateSellerMch();
		token=AuthCallServiceV2.createToken(sellerId, "app");
		csId = GodmchManageCallService.bindClientService(
				sellerId, 1);
		cstoken = AuthCallServiceV2.createToken(csId, "app");
	}

	@Before
	public void caseUp() {
		queryproductlistBean=new QueryProductListBean();
		queryproductlistCall=new QueryProductListCall();
		liveRoomShop2nWapper = new LiveRoomShop2cnWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(queryproductlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryProductList_001() {
		Logger.start(true,"获取某直播间商品列表成功");
		try {
			LiveRoom createlive = LiveRoomShop2cnCallService.CreateSellerLive(LiveRoomShop2cnCallService.initCreatesellerliveBean(),sellerId);
			LiveRoomShop2cnCallService.SetisLiveinherit(sellerId, createlive.getId(), 1);
			Integer InheritUserId=GodmchManageCallService.createAgent(sellerId);
			LiveRoomShop2cnCallService.AddLiveToShop(createlive.getId(), InheritUserId, sellerId);
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProductV2(sellerId, "autotest",
					false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Logger.comment("创建商品：" + pid);
			LiveRoomShop2cnCallService.AddProductsToLive(createlive.getId(),  sellerId, Arrays.asList(pid));
			Thread.sleep(3000);
			queryproductlistBean.setLiveId(createlive.getId());
			queryproductlistBean.setPageIndex(1);
			queryproductlistBean.setPageSize(10);
			queryproductlistBean.setShopId(sellerId);
			queryproductlistCall.setData(queryproductlistBean);
			queryproductlistCall.callService();			
			Logger.verifyEquals("200", queryproductlistCall.getString("code"), "code");
			JsonArray productList = queryproductlistCall.getJsonObject().get("data").getAsJsonObject().get("productList").getAsJsonArray();
			Logger.verifyEquals(1, productList.size(), "验证size");
			Logger.verifyEquals(pid, productList.get(0).getAsJsonObject().get("productId").getAsString(), "验证productId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryProductList_002() {
		Logger.start(true,"获取代理上级联播直播间商品列表成功");
		try {
			//没有设置自动加价的时候获取商家商品id直接加入直播列表
			LiveRoom createlive = LiveRoomShop2cnCallService.CreateSellerLive(LiveRoomShop2cnCallService.initCreatesellerliveBean(),sellerId);
			LiveRoomShop2cnCallService.SetisLiveinherit(sellerId, createlive.getId(), 1);
			Integer InheritUserId=GodmchManageCallService.createAgent(sellerId);
			LiveRoomShop2cnCallService.AddLiveToShop(createlive.getId(), InheritUserId, sellerId);
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProductV2(sellerId, "autotest",
					false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Logger.comment("创建商品：" + pid);
			LiveRoomShop2cnCallService.AddProductsToLive(createlive.getId(),  sellerId, Arrays.asList(pid));
			Thread.sleep(3000);
//			String pid2=LiveRoomShop2cnCallService.getMyProductId(pid, InheritUserId);
//			if(pid2==null) {
//				Thread.sleep(3000);
//				pid2=LiveRoomShop2cnCallService.getMyProductId(pid, InheritUserId);
//			}
			queryproductlistBean.setLiveId(createlive.getId());
			queryproductlistBean.setPageIndex(1);
			queryproductlistBean.setPageSize(10);
			queryproductlistBean.setShopId(InheritUserId);
			queryproductlistCall.setData(queryproductlistBean);
			queryproductlistCall.callService();	
			Logger.verifyEquals("200", queryproductlistCall.getString("code"), "code");
			JsonArray productList = queryproductlistCall.getJsonObject().get("data").getAsJsonObject().get("productList").getAsJsonArray();
			Logger.verifyEquals(1, productList.size(), "验证size");
			Logger.verifyEquals(pid, productList.get(0).getAsJsonObject().get("productId").getAsString(), "验证productId");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	// com.shop2cn.iapi.agentproducts.testcase.Ts_ProductDetails.Tc_ProductDetails_058()
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryProductList_003() {
		Logger.start(true,"获取代理上级联播直播间商品列表成功-设置自动加价");
		try {
			//没有设置自动加价的时候获取商家商品id直接加入直播列表
			LiveRoom createlive = LiveRoomShop2cnCallService.CreateSellerLive(LiveRoomShop2cnCallService.initCreatesellerliveBean(),sellerId);
			//创建代理
			Integer InheritUserId=GodmchManageCallService.createAgent(sellerId);
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProductV2(sellerId, "autotest",
					false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Logger.comment("创建商品：" + pid);
			//设置自动加价规则
			setConditionMarkupConfigBean setConditionMarkupConfigBean = GodmchManageCallService.initDefSetConditionMarkupConfig(InheritUserId, sellerId, 1, addAgentProductBean, 0);
            setConditionMarkupConfigBean.setRefreshProduct(true);
            GodmchManageCallService.SetConditionMarkupConfigCall(setConditionMarkupConfigBean);
            
            //设置直播继承
			LiveRoomShop2cnCallService.SetisLiveinherit(sellerId, createlive.getId(), 1);
			
			LiveRoomShop2cnCallService.AddLiveToShop(createlive.getId(), InheritUserId, sellerId);
			
			
			
			LiveRoomShop2cnCallService.AddProductsToLive(createlive.getId(),  sellerId, Arrays.asList(pid));
			Thread.sleep(3000);
			String pid2=LiveRoomShop2cnCallService.getMyProductId(pid, InheritUserId);
			if(pid2==null) {
				Thread.sleep(3000);
				pid2=LiveRoomShop2cnCallService.getMyProductId(pid, InheritUserId);
			}
			queryproductlistBean.setLiveId(createlive.getId());
			queryproductlistBean.setPageIndex(1);
			queryproductlistBean.setPageSize(10);
			queryproductlistBean.setShopId(InheritUserId);
			queryproductlistCall.setData(queryproductlistBean);
			queryproductlistCall.callService();	
			Logger.verifyEquals("200", queryproductlistCall.getString("code"), "code");
			JsonArray productList = queryproductlistCall.getJsonObject().get("data").getAsJsonObject().get("productList").getAsJsonArray();
			Logger.verifyEquals(1, productList.size(), "验证size");
			Logger.verifyEquals(pid2, productList.get(0).getAsJsonObject().get("productId").getAsString(), "验证productId");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P2.class})
	@TestCase
	// com.shop2cn.iapi.agentproducts.testcase.Ts_ProductDetails.Tc_ProductDetails_058()
	public void Tc_QueryProductList_004() {
		Logger.start(true,"获取代理上级联播直播间商品列表成功-多级代理设置自动加价");
		try {
			//创建vip3级代理
			Integer sellerId1 = GodmchManageCallService.CreateGodMch();
			GodmchManageCallService.SetVip(sellerId1);
			Integer InheritUserId = GodmchManageCallService.CreateAgent(sellerId1);
			Integer InheritUserId1 = GodmchManageCallService.CreateAgent(InheritUserId,sellerId1);
			LiveRoom createlive = LiveRoomShop2cnCallService.CreateSellerLive(LiveRoomShop2cnCallService.initCreatesellerliveBean(),sellerId1);
			
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProductV2(sellerId1, "autotest",
					false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Logger.comment("创建商品：" + pid);
			
			//设置自动加价规则
			setConditionMarkupConfigBean setConditionMarkupConfigBean = GodmchManageCallService.initDefSetConditionMarkupConfig(InheritUserId, sellerId1, 1, addAgentProductBean, 0);
            setConditionMarkupConfigBean.setRefreshProduct(true);
            GodmchManageCallService.SetConditionMarkupConfigCall(setConditionMarkupConfigBean);
          //设置自动加价规则2
			setConditionMarkupConfigBean setConditionMarkupConfigBean2 = GodmchManageCallService.initDefSetConditionMarkupConfig(InheritUserId1, InheritUserId, 1, addAgentProductBean, 0);
            setConditionMarkupConfigBean.setRefreshProduct(true);
            GodmchManageCallService.SetConditionMarkupConfigCall(setConditionMarkupConfigBean2);
            
            //设置直播继承
			LiveRoomShop2cnCallService.SetisLiveinherit(sellerId1, createlive.getId(), 1);
			
			LiveRoomShop2cnCallService.AddLiveToShop(createlive.getId(), InheritUserId, sellerId1);
			
			
			
			LiveRoomShop2cnCallService.AddProductsToLive(createlive.getId(),  sellerId1, Arrays.asList(pid));
			Thread.sleep(3000);
			String pid2=LiveRoomShop2cnCallService.getMyProductId(pid, InheritUserId);
			if(pid2==null) {
				Thread.sleep(3000);
				pid2=LiveRoomShop2cnCallService.getMyProductId(pid, InheritUserId);
			}
			String pid3=LiveRoomShop2cnCallService.getMyProductId(pid2, InheritUserId1);
			if(pid3==null) {
				Thread.sleep(3000);
				pid3=LiveRoomShop2cnCallService.getMyProductId(pid2, InheritUserId1);
			}
			queryproductlistBean.setLiveId(createlive.getId());
			queryproductlistBean.setPageIndex(1);
			queryproductlistBean.setPageSize(10);
			queryproductlistBean.setShopId(InheritUserId);
			queryproductlistCall.setData(queryproductlistBean);
			queryproductlistCall.callService();	
			Logger.verifyEquals("200", queryproductlistCall.getString("code"), "code");
			JsonArray productList = queryproductlistCall.getJsonObject().get("data").getAsJsonObject().get("productList").getAsJsonArray();
			Logger.verifyEquals(1, productList.size(), "验证size");
			Logger.verifyEquals(pid2, productList.get(0).getAsJsonObject().get("productId").getAsString(), "验证productId");
			Thread.sleep(3000);
			queryproductlistBean.setLiveId(createlive.getId());
			queryproductlistBean.setPageIndex(1);
			queryproductlistBean.setPageSize(10);
			queryproductlistBean.setShopId(InheritUserId1);
			queryproductlistCall.setData(queryproductlistBean);
			queryproductlistCall.callService();	
			Logger.verifyEquals("200", queryproductlistCall.getString("code"), "code");
			JsonArray productList2 = queryproductlistCall.getJsonObject().get("data").getAsJsonObject().get("productList").getAsJsonArray();
			Logger.verifyEquals(1, productList2.size(), "验证size");
			Logger.verifyEquals(pid3, productList2.get(0).getAsJsonObject().get("productId").getAsString(), "验证productId");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}